package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username1 = employeeLoginDTO.getUsername();
        String password1 = employeeLoginDTO.getPassword();
        //1 根据用户名查找数据库数据
        Employee byUsername = employeeMapper.getByUsername(username1);
        // 2处理各种异常
        //账号不存在
        if (byUsername==null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码错误
        //TODO 密码加密操作
//        password1 = DigestUtils.md5Hex(password1);

        if(!password1.equals(byUsername.getPassword())){
            //密码错误
            throw  new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        //账号锁定
        if (byUsername.getStatus()== StatusConstant.DISABLE){
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return byUsername;
    }

    /**
     * 新增员工
     *
     * @param employeeDTO
     */
    @Override
    public void addEmployee(EmployeeDTO employeeDTO) {
        //将DTO封装的数据 封装到Employee中，持久化到数据库
        //1使用beanUtils
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        //2补全employee数据
        employee.setPassword(DigestUtils.md5Hex("123456"));//临时密码
        employee.setCreateTime(LocalDateTime.now());//创建时间
        employee.setUpdateTime(LocalDateTime.now());//更新时间
        employee.setCreateUser(BaseContext.getCurrentId());//TODO，临时值，需要获取当前操作人，
        employee.setUpdateUser(BaseContext.getCurrentId());
        employee.setStatus(StatusConstant.ENABLE);
        //持久化到数据库
        employeeMapper.insert(employee);

    }

    /**
     * 员工分页查询
     *
     * @param employeePageQueryDTO
     * @return
     */
    @Override
    public PageResult findPage(EmployeePageQueryDTO employeePageQueryDTO) {

        System.err.println("111111111111");
        //使用分页插件
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        //查询数据库数据
        Page<Employee> page = employeeMapper.findPage(employeePageQueryDTO);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    /**
     * 启用禁用员工
     *
     * @param stasus
     * @param id
     */
    @Override
    public void starOrStop(Integer stasus, Long id) {
        Employee employee = Employee.builder().id(id).status(stasus).build();

        employeeMapper.update(employee);
    }

    /**
     * 跟新员工信息
     *
     * @param employeeDTO
     * @return
     */
    @Override
    public void update(EmployeeDTO employeeDTO) {
        //将DTO封装的数据 封装到Employee中，持久化到数据库
        //1使用beanUtils
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        //2补全employee数据
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(BaseContext.getCurrentId());

        //持久化到数据库
        employeeMapper.update(employee);
    }

    /**
     * 根据id查询员工
     *
     * @param id
     * @return
     */
    @Override
    public Employee getBuId(Long id) {
        Employee employee = employeeMapper.getById(id);
        employee.setPassword("******");
        return employee;
    }

    /**
     * 修改员工密码
     *
     * @param passwordEditDTO
     * @return
     */
    @Override
    public void passWordEdit(PasswordEditDTO passwordEditDTO) {
        //1获取传递的新旧密码
        String oldPassword = passwordEditDTO.getOldPassword();
        String newPassword = passwordEditDTO.getNewPassword();
        //2通过线程获取当前操作人id
        Long currentId = BaseContext.getCurrentId();
        //3通过id获取旧密码与接受的旧密码对比
        Employee employeeMapperById = employeeMapper.getById(currentId);
        String password = employeeMapperById.getPassword();
        Long id = employeeMapperById.getId();
        //TODO 登录没有做加密 这里也暂时不做加密
        //4判断旧密码是否正确
        if (!oldPassword.equals(password)){
            throw new PasswordErrorException(MessageConstant.OLD_PASSWORD_ERROR);
        }else {
            //输入的旧密码正确，更换密码
            Employee employee = Employee.builder().id(id).password(newPassword).build();
            employeeMapper.update(employee);
        }

    }


}
